<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="instanceName">
        <el-input v-model="queryParams.instanceName" placeholder="请输入流程名称" clearable style="width: 240px"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <el-table :data="tableDataList" @selection-change="handleSelectionChange" border align="center" header-align="center" :height="tableHeight" v-loading="loading">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id"/>
      <el-table-column prop="instanceName" label="流程名称" align="center"></el-table-column>
      <el-table-column prop="businessKey" label="流程类型" align="center"></el-table-column>
      <el-table-column prop="startTime" label="开始时间" align="center">
        <template slot-scope="scope">
          <span>{{ formattedTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column prop="assignee" label="办理人" align="center"></el-table-column>-->
      <el-table-column prop="endTime" label="办结时间" align="center">
        <template slot-scope="scope">
          <span>{{ formattedTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  type="text" @click="handleApprove(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 详情页面 -->
    <!-- 归档申请单 istype="1"   待办 istype="2"  已办 istype="3" -->
    <approve :title="title" :open="approveOpen" istype="3" @closeDelDialog="closeDelDialog" ref="approveRef"></approve>
  </div>
</template>
<script>
import estimateTableHeight from '@/mixins/estimateTableHeight'
import {getProcessType, taskHistoryList} from "@/api/archive/archiveBorrow/borrowReceipt.js";
import approve from "@/views/archive/archiveBorrow/archiveBorrowReceipt/notQrocessed/approve";
//vue项目中element-tabel自动计算高度
export default {
  components: { approve },
  mixins: [estimateTableHeight], // 混入方法
  data() {
    return {
      queryParams: {
        pageNum:1,
        pageSize:10,
      },
      showSearch: true, // 显示搜索条件
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格数据
      tableDataList: [],
      loading: true,
      total: 0,
      // 流程类型
      typeOption: [],
      // 详情页面
      approveOpen: false,
      title: "详情"
    }
  },
  created() {
    this.getOption()
    this.getList()
  },
  methods: {
    formattedTime(time) {
      return new Date(time).toLocaleString();
    },
    // 获取流程类型
    getOption() {
      getProcessType().then(response=>{
        const data = response.data
        for (const key in data) {
          this.typeOption.push({
            value: key,
            label: data[key]
          });
        }
        console.log(this.typeOption)
      })
    },
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    getList() {
      this.loading = true
      taskHistoryList(this.queryParams).then(response=>{
        // console.log(response)
        this.tableDataList = response.rows
        for (let i = 0; i < response.rows.length; i++) {
          for (let j = 0; j < this.typeOption.length; j++) {
            if(response.rows[i].businessKey == this.typeOption[j].value) {
              this.tableDataList[i].businessKey = this.typeOption[j].label
            }
          }
          const date = new Date(response.rows[i].startTime);
          // 获取日期的年、月、日
          const year = date.getFullYear();
          const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
          const day = String(date.getDate()).padStart(2, '0');
          // 获取时间的小时、分钟、秒
          const hours = String(date.getHours()).padStart(2, '0');
          const minutes = String(date.getMinutes()).padStart(2, '0');
          const seconds = String(date.getSeconds()).padStart(2, '0');
          // 构建日期时间字符串
          this.tableDataList[i].startTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        }
        this.total = response.total
        this.loading = false
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 详情
    handleApprove(row) {
      // console.log(row)
      this.$nextTick(() => {
        this.$refs.approveRef.init(row);
      })
      this.approveOpen = true
    },
    //关闭
    closeDelDialog() {
      this.approveOpen = false
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>
